package com.itheima.im.socket.service;

import com.itheima.im.socket.activity.MyApp;
import com.itheima.im.socket.bean.QQMessage;
import com.itheima.im.socket.bean.QQMessageType;
import com.itheima.im.socket.core.OnReceiveMsgListener;
import com.itheima.im.socket.utils.ThreadUtils;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

//四大组件 1.继承 2.重写  3.配置 4.打开
public class ChatService extends Service {

	@Override
	public void onCreate() {
		super.onCreate();// Notification QQ
		Toast.makeText(this, "聊天..后台服务.", Toast.LENGTH_SHORT).show();
		MyApp.conn.addOnReceiveMsgListener(listener);
	}

	private OnReceiveMsgListener listener=new OnReceiveMsgListener() {

		@Override
		public void onReceive(final QQMessage msg) {//子线程

			ThreadUtils.runUIThread(new Runnable() {

				@Override
				public void run() {
					if(msg.type.equals(QQMessageType.MSG_TYPE_CHAT_P2P)) {
						Toast.makeText(getBaseContext(), "好友消息."+msg.content, 0).show();
					}

				}
			});
		}
	};
	@Override
	public void onDestroy() {
		super.onDestroy();
		MyApp.conn.removeOnReceiveMsgListener(listener);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

}
